﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using VPIC_EDILib;
namespace VPIC_EDIGUI
{
    public partial class EDIProcess : DevExpress.XtraEditors.XtraForm
    {
        string type = "";
        Config itemConfig = new Config();
        public EDIProcess()
        {
            InitializeComponent();
            itemConfig = itemConfig.LoadConfig();
        }

        private void btnOpenFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DialogResult result = this.openFileDialog_FileCSV.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.btnOpenFile.Text = this.openFileDialog_FileCSV.FileName;
            }
        }

        private void openFileDialog_FileCSV_FileOk(object sender, CancelEventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(itemConfig.ShareFolderCSV);
            FileInfo fileCSV = new FileInfo(this.openFileDialog_FileCSV.FileName);
            StreamReader reader = fileCSV.OpenText();
          
            int i = 0;
            string content = "";
            while (reader.Peek() > 0)
            {
             
                if (i > 0)
                {
                    string line = reader.ReadLine();
                    content += line + "\r\n";
                    string[] tokens = line.Split(new char[1] { ',' });
                    type = tokens[0];
                }
                i++;
            }
            reader.Close();
         

           
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            this.progressPanel1.Visible = true;
            this.btnProcess.Enabled = false;
            this.btnOpenFile.Enabled = false;
            this.bgw.RunWorkerAsync();
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(itemConfig.ShareFolderCSV);
            FileInfo fileCSV = new FileInfo(this.openFileDialog_FileCSV.FileName);
            XMLProcess XMLProcess = new XMLProcess(itemConfig);


            if (type == "830")
            {
                List<EDIEntity> ListEDI = new List<EDIEntity>();
                ListEDI = XMLProcess.GetListEDIEntitySOForecast(fileCSV);
                string filename = XMLProcess.GetXMLSOForecast(ListEDI);
                XMLProcess.StartProcessImportInvoice(filename);
                //fileCSV.MoveTo(dirInfo.FullName + "\\Imported\\" + fileCSV.Name + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));
               
            }
            if (type == "862")
            {
                List<EDIEntityShip> ListEDI = new List<EDIEntityShip>();
                ListEDI = XMLProcess.GetListEDIEntitySOShip(fileCSV);
                string filename = XMLProcess.GetXMLSOShippingSchedules(ListEDI);
                XMLProcess.StartProcessImportInvoice(filename);
                //fileCSV.MoveTo(dirInfo.FullName + "\\Imported\\" + fileCSV.Name + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));
               
            }
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.progressPanel1.Visible = false;
            this.btnProcess.Enabled = true;
            this.btnOpenFile.Enabled = true;
            MessageBox.Show("Converted & Imported Exact Successfully");
        }
    }
}